var listCities = [];
var listDistricts = [];

$(function() {
	$("#dateOfBirth").datepicker({dateFormat: "dd/mm/yy"}).val($("#dateOfBirth").val());
	// get list city
	$('ul#listCities li').each(function(index){
		var tmpObj = {};
		tmpObj.value = $(this).text();
		tmpObj.id = $(this).val();
		listCities.push(tmpObj);
	});
	
	
	// get list district
	$('ul#listDistricts li').each(function(index){
		var tmpObj = {};
		tmpObj.value = $(this).text();
		tmpObj.id = $(this).val();
		tmpObj.belongTo = $(this).data('belongCities');
		listDistricts.push(tmpObj);
	});
	
  // onselect tinh/thanh pho dropdownlist -> load quan huyen tuong ứng
	var wardSelectFn = function(){
		var optionSelected = $("select#belongToWard option:selected")[0];
		var idDistrict = $(optionSelected).data("belongdistrict");
		var districtObj;
		var citiObj;
		if(listDistricts != undefined && listDistricts.length > 0) {
		  for(var i=0;i<listDistricts.length;i++) {
			  if(listDistricts[i].id == idDistrict) {
				  
				  districtObj = listDistricts[i];
				  if(listDistricts != undefined && listDistricts.length > 0) {
					  for(var j=0;j<listCities.length;j++) {
						  if(districtObj.belongTo == listCities.id) {
							  citiObj = listCities[i];
							  break;
						  }
					  }
				  }
				  break;
			  }
			  
		  }
		  
		  if(districtObj && citiObj) {
			  $('#wardDetails').html("<span>Huyện&nbsp" + districtObj.value +",&nbsp Tỉnh&nbsp" + citiObj.value +"</span>");
		  }
		}

	};
	wardSelectFn();
	$('select#belongToWard').change(wardSelectFn);
});